package com.info33.platform.common.mvc.controller;

import com.info33.platform.common.exception.NoCertificationException;
import com.info33.platform.common.exception.ResultException;
import com.info33.platform.common.exception.TransactionalException;
import com.info33.platform.common.mvc.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * @author debj
 * @version 1.0
 * @date 2021/7/26 15:06
 */
@Slf4j
@ControllerAdvice
public class ExceptionController {

    @Value("${spring.servlet.multipart.max-file-size}")
    private String maxFileSize;
    @Value("${spring.servlet.multipart.max-request-size}")
    private String maxRequestSize;

    @ResponseBody
    @ExceptionHandler(value = AuthorizationException.class)
    public Result<Object> transactionalException(AuthorizationException e) {
        log.warn(e.getMessage());
        return Result.error("您的操作受限！原因：权限不足");
    }

    @ResponseBody
    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    public Result<Object> maxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        log.warn(e.getMessage());
        return Result.error("您上传的文件过大，单文件最大支持 " + maxFileSize + "，单次上传最大支持 " + maxRequestSize);
    }

    @ResponseBody
    @ExceptionHandler(value = TransactionalException.class)
    public Result<Object> transactionalException(TransactionalException e) {
        return Result.error(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = ResultException.class)
    public Result<Object> exception(ResultException e) {
        e.printStackTrace();
        return Result.error(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = NoCertificationException.class)
    public Result<Object> noCertificationException() {
        return Result.noCertified();
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result<Object> exception(Exception e) {
        e.printStackTrace();
        return Result.error(Result.RESULT_ERROR_PUBLIC_MSG, e.getMessage());
    }

}